home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 4 / The Arsenal Files 4 (Arsenal Computer).ISO / ham / sattrk31.tgz / sattrack-3.1.tar / SatTrack / src / maketles / maketles.c
C/C++ Source or Header  |  1995-03-16  |  10KB  |  282 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /*  Title       : maketles.c                                                  */
  4. /*  Author      : Manfred Bester                                              */
  5. /*  Date        : 10Feb93                                                     */
  6. /*  Last change : 15Mar95                                                     */
  7. /*                                                                            */
  8. /*  Synopsis    : This program reads a file with Keplerian elements in the    */
  9. /*                NORAD 2-line format and selects certain satellites out of   */
  10. /*                these. Their data will be written into a new data file.     */
  11. /*                                                                            */
  12. /*                                                                            */
  13. /*  Command line argument: satellite group                                    */
  14. /*                                                                            */
  15. /*  Input files : satlist_nnn.dat, tlex.dat                                   */
  16. /*  Output file : tles.dat                                                    */
  17. /*                                                                            */
  18. /*                                                                            */
  19. /*  SatTrack is Copyright (c) 1992, 1993, 1994, 1995 by Manfred Bester.       */
  20. /*  All Rights Reserved.                                                      */
  21. /*                                                                            */
  22. /*  Permission to use, copy, and distribute SatTrack and its documentation    */
  23. /*  in its entirety for educational, research and non-profit purposes,        */
  24. /*  without fee, and without a written agreement is hereby granted, provided  */
  25. /*  that the above copyright notice and the following three paragraphs appear */
  26. /*  in all copies. SatTrack may be modified for personal purposes, but        */
  27. /*  modified versions may NOT be distributed without prior consent of the     */
  28. /*  author.                                                                   */
  29. /*                                                                            */
  30. /*  Permission to incorporate this software into commercial products may be   */
  31. /*  obtained from the author, Dr. Manfred Bester, 1636 M. L. King Jr. Way,    */
  32. /*  Berkeley, CA 94709, USA. Note that distributing SatTrack 'bundled' in     */
  33. /*  with ANY product is considered to be a 'commercial purpose'.              */
  34. /*                                                                            */
  35. /*  IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, */
  36. /*  SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF   */
  37. /*  THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE AUTHOR HAS BEEN ADVISED  */
  38. /*  OF THE POSSIBILITY OF SUCH DAMAGE.                                        */
  39. /*                                                                            */
  40. /*  THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT      */
  41. /*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A   */
  42. /*  PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"      */
  43. /*  BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, */
  44. /*  UPDATES, ENHANCEMENTS, OR MODIFICATIONS.                                  */
  45. /*                                                                            */
  46. /******************************************************************************/
  47.  
  48. #include <stdio.h>
  49. #include <math.h>
  50. #include <string.h>
  51.  
  52. #ifndef STDLIB
  53. #include <stdlib.h>
  54. #endif
  55.  
  56. #include "sattrack.h"
  57.  
  58. #ifdef HPTERM
  59. #include "hpterm.h"
  60. #else
  61. #include "vt100.h"
  62. #endif
  63.  
  64. extern double getElement();
  65. extern void   mGets(), truncBlanks(), upperCase();
  66.  
  67. void main(argc,argv)
  68.  
  69. int  argc;
  70. char *argv[];
  71.  
  72. {
  73.     long   objectNum;
  74.  
  75.     int    lineNum1, lineNum2;
  76.  
  77.     char   satListName[100], satName[100], line0[100], line1[100], line2[100];
  78.     char   data[100], tle[100];
  79.     char   inputSatFile[100], inputTleFile[100], outputFile[100], satGroup[10];
  80.     char   *strpHome, *getenv();
  81.  
  82.     FILE   *InputSatFile, *InputTleFile, *OutputFile;
  83.  
  84. #ifdef HOMEDIR
  85.     strpHome = getenv("HOME");
  86. #else
  87.     strpHome = SATDIR;
  88. #endif
  89.  
  90.     if (argc == 2)
  91.     {
  92.         strcpy(satGroup,argv[1]);
  93.     }
  94.  
  95.     else
  96.     {
  97.         printf("enter satellite group (am,com,gps,sci,tv,wx,...): ");
  98.         mGets(satGroup);
  99.     }
  100.  
  101.     sprintf(data,"%s/%s",strpHome,DATA);
  102.     sprintf(tle,"%s/%s",strpHome,TLE);
  103.     sprintf(inputSatFile,"%s/satlist_%s.dat",data,satGroup);
  104.     sprintf(inputTleFile,"%s/tlex.dat",tle);
  105.     sprintf(outputFile,"%s/tles.dat",tle);
  106.  
  107.     if ((InputSatFile = fopen(inputSatFile,"r")) == NULL)
  108.     {
  109.         nl(); doBeep(); reverseBlink();
  110.         printf("%s not found\n",inputSatFile);
  111.         normal(); nl();
  112.         exit(-1);
  113.     }
  114.  
  115.     if ((InputTleFile = fopen(inputTleFile,"r")) == NULL)
  116.     {
  117.         nl(); doBeep(); reverseBlink();
  118.         printf("%s not found\n",inputTleFile);
  119.         normal(); nl();
  120.         exit(-1);
  121.     }
  122.  
  123.     else
  124.         fclose(InputTleFile);
  125.  
  126.     if ((OutputFile = fopen(outputFile,"w")) == NULL)
  127.     {
  128.         nl(); doBeep(); reverseBlink();
  129.         printf("can't write %s\n",outputFile);
  130.         normal(); nl();
  131.         exit(-1);
  132.     }
  133.  
  134.     while (fgets(satListName,80,InputSatFile))
  135.     {
  136.         satListName[strlen(satListName)-1] = '\0';
  137.         upperCase(satListName);
  138.  
  139.         InputTleFile = fopen(inputTleFile,"r");
  140.  
  141.         while (fgets(line0,100,InputTleFile))
  142.         {
  143.             if (!strncmp(line0,TLEHEADER,10))
  144.                 fgets(line0,100,InputTleFile);
  145.  
  146.             strcpy(satName,line0);
  147.             satName[strlen(satName)-1] = '\0';
  148.             truncBlanks(satName);
  149.             upperCase(satName);
  150.  
  151.             fgets(line1,100,InputTleFile);
  152.             fgets(line2,100,InputTleFile);
  153.  
  154.             sscanf(line1,"%1d",&lineNum1);
  155.             sscanf(line2,"%1d",&lineNum2);
  156.  
  157.             if (lineNum1 != 1)
  158.                 printf("Line 1 not available for satellite %s",satName);
  159.  
  160.             if (lineNum2 != 2)
  161.                 printf("Line 2 not available for satellite %s",satName);
  162.  
  163.             objectNum = (long) (getElement(line1,3,7) + ONEPPM);
  164.  
  165.             if (!strcmp(satListName,satName) || objectNum == atol(satListName))
  166.             {
  167.                 if (lineNum1 == 1 && lineNum2 == 2)
  168.                 {
  169.                     fprintf(OutputFile,"%s",line0);
  170.                     fprintf(OutputFile,"%s",line1);
  171.                     fprintf(OutputFile,"%s",line2);
  172.                 }
  173.             }
  174.         }
  175.  
  176.         fclose(InputTleFile);
  177.     }
  178.  
  179.     fclose(InputSatFile);
  180.     fclose(OutputFile);
  181. }
  182.  
  183. /******************************************************************************/
  184. /*                                                                            */
  185. /* truncBlanks: truncates trailing blanks from character string               */
  186. /*                                                                            */
  187. /******************************************************************************/
  188.  
  189. void truncBlanks(string)
  190.  
  191. char *string;
  192.  
  193. {
  194.     int i;
  195.  
  196.     i = strlen(string) - 1;
  197.  
  198.     while (string[i] == ' ')
  199.     {
  200.         i--;
  201.     }
  202.  
  203.     string[i+1] = '\0';                          /* add termination character */
  204.     return;
  205. }
  206.  
  207. /******************************************************************************/
  208. /*                                                                            */
  209. /* upperCase: changes lower to upper case letters                             */
  210. /*                                                                            */
  211. /******************************************************************************/
  212.  
  213. void upperCase(string)
  214.  
  215. char *string;
  216.  
  217. {
  218.     int i;
  219.  
  220.     for (i = 0; i < strlen(string); i++)
  221.     {
  222.         if (string[i] >= 'a' && string[i] <= 'z')
  223.             string[i] -= 'a' - 'A';
  224.     }
  225.  
  226.     return;
  227. }
  228.  
  229. /******************************************************************************/
  230. /*                                                                            */
  231. /* getElement: returns double of orbital element out of ASCII string          */
  232. /*                                                                            */
  233. /******************************************************************************/
  234.  
  235. double getElement(gstr,gstart,gstop)
  236.  
  237. int  gstart, gstop;
  238. char gstr[80];
  239.  
  240. {
  241.     int  k, glength;
  242.     char gestr[80];
  243.  
  244.     glength = gstop - gstart + 1;
  245.  
  246.     for (k = 0; k <= glength; k++)
  247.         gestr[k] = gstr[gstart+k-1];
  248.  
  249.     gestr[glength] = '\0';
  250.  
  251.     return((double) atof(gestr));
  252. }
  253.  
  254. /******************************************************************************/
  255. /*                                                                            */
  256. /* mGets: Manfred's version of fgets (wipes out newline character)            */
  257. /*                                                                            */
  258. /******************************************************************************/
  259.  
  260. void mGets(string)
  261.  
  262. char *string;
  263.  
  264. {
  265.     int i;
  266.     fgets(string,80,stdin);
  267.     i = (int) strlen(string);
  268.  
  269.     if (i > 0)
  270.         string[i-1] = '\0';
  271.     else
  272.         string[0]   = '\0';
  273.  
  274.     return;
  275. }
  276.  
  277. /******************************************************************************/
  278. /*                                                                            */
  279. /* End of program maketles.c                                                  */
  280. /*                                                                            */
  281. /******************************************************************************/
  282.